//
//  WBStatusViewModel.swift
//  ZWWEIBO
//
//  Created by xzw on 2018/4/9.
//  Copyright © 2018年 xzw. All rights reserved.
//

import UIKit

//只有通过kvc赋值才需要继承nsobject


//表格性能优化 1.尽量少计算，所需要素材提前计算好 2.控件上不要设置圆角，图像渲染属性 3，不要动态创建控件，提前创建好，显示的时候隐藏／显示  4.cell控件层次越少越好



//单条微博的视图模型
class WBStatusViewModel:CustomStringConvertible {
    
   
    //微博模型
    var status:WBStatus
    
    //会员图标
    var memberIcon:UIImage?
    
    //认证类型，-1:没有认证，0:认证用户，2、3、5:企业认证，220:达人
    var vipIcon:UIImage?
    
    //转发文字
    var retweetedStr: String?
    
    //评论文字
    var commentStr: String?
    
    //点赞文字
    var likeStr: String?
    
    //配图视图大小
    var pictureViewSize = CGSize.zero
    
    //如果是被转发的微博，原创微博一定没有图
    var picURLs:[WBStatusPicture]? {
        return status.retweeted_status?.pic_urls ?? status.pic_urls
    }
    
    //被转发微博文字
    var retweetedText: String?
    
    //构造函数
    init(model:WBStatus){
     self.status = model
        
        
        //会员等级1-6
       //common_icon_membership_level1
        var  imageName = ""
        if (model.user?.mbrank ?? 0) > 0 && (model.user?.mbrank ?? 0) < 7 {
            imageName = "common_icon_membership_level\(model.user?.mbrank ?? 1)"
            
        }else
        {
           imageName = "common_icon_membership_level1"
        }
        memberIcon = UIImage(named: imageName)
        
        //认证图标
        switch model.user?.verified_type ?? -1 {
        case 0:
            vipIcon = UIImage(named: "avatar_vip")
            
        case 2,3,5:
            vipIcon = UIImage(named: "avatar_enterprise_vip")
            
        case 220:
            vipIcon = UIImage(named: "avatar_grassroot")
            
        default:
            break
        }
        
        //设置底部计数字符串
        retweetedStr = countString(count: model.reposts_count, defaultStr: "转发")
        commentStr = countString(count: model.comments_count, defaultStr: "评论")
        likeStr = countString(count: model.attitudes_count, defaultStr: "赞")
        
        //计算配图视图大小,有原创计算原创，有转发计算转发
        pictureViewSize = calCPictureViewSize(count: picURLs?.count ?? 0 )
        print("九宫格图片：\(model.pic_urls?.count ?? 0)-----\(pictureViewSize)")
        
        //设置被转发微博的文字
        retweetedText = "@" + (status.retweeted_status?.user?.screen_name ?? "") + ":"
        retweetedText = retweetedText! + (status.retweeted_status?.text ?? "")
        
    }
    
    var description: String{
        return status.description
    }
    
    
    /// 如果是用单张图片，更改配图视图大小
    ///
    /// - Parameter image: 网络缓存的单张图片
    func updateSingleImageSize(image:UIImage){
     
        var size = image.size
        
        //注意，尺寸需要增加顶部12个点
        size.height = WBPicViewOutMargin + size.height
        
        pictureViewSize = size
    }
    
    /// 计算指定数量的图片对应的配图视图的大小
    ///
    /// - Parameter count: 图片数量
    /// - Returns: 返回九宫格视图的尺寸大小
    private func calCPictureViewSize(count: Int) -> CGSize {
        
        if (count == 0)  {
            
            return CGSize.zero
        }
        
        //计算视图的高度,根据count 计算行数
        /*
           1 2 3 = 0 1 2 / 3 = 0 + 1 = 1
           4 5 6 = 3 4 5 / 3 = 1 + 1 = 2
           7 8 9 = 6 7 8 / 3 = 2 + 1 = 3
         
         */
        let row = (count - 1) / 3 + 1
        
        //根据行数算高度
        let pictreH = WBPicViewOutMargin + CGFloat(row) * WBPictureItemW + CGFloat((row - 1)) * WBPicViwInnerMargin
        
        
      return CGSize(width: WBPictureW, height: pictreH)
    }
    
    
    /// 给定一个数字，返回对应的描述信息
    ///
    /// - Parameters:
    ///   - count: 数字
    ///   - defaultStr: 默认显示字符串
    /// - Returns: 描述结果
    private func countString(count: Int,defaultStr: String) -> String {
        
        if count == 0 {
            return defaultStr
        }
        if count < 10000 {
            return count.description
        }
        return String(format: "%.02f 万", Double(count) / 10000)
    }
}
